﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using uam_fps_game.ParticleSystems;

namespace uam_fps_game.Gfx
{
    public class GraphicsSystem
    {
        public static GraphicsSystem Global { get; set; }

        public VisualSceneData SceneData { get; private set; }

        public GraphicsSystem(GraphicsDevice device, IServiceProvider serviceProvider)
        {
            Content = new ContentManager(serviceProvider, "Content");
            _renderer = new Renderer(device, serviceProvider);
            SceneData = new VisualSceneData();
        }

        public ModelInstance CreateModelInstance(string modelFilePath)
        {
            ModelObject model = null;
            if (_modelMap.ContainsKey(modelFilePath))
            {
                model = _modelMap[modelFilePath];
            }
            else
            {
                model = new ModelObject(modelFilePath, Content);
                _modelMap[modelFilePath] = model;
            }
            ModelInstance modelInst = new ModelInstance(model);
            SceneData.Models.Add(modelInst);

            return modelInst;
        }

        public Billboard CreateBillboard(string textureFileName)
        {
            Billboard billboard = new Billboard(Content.Load<Texture2D>(textureFileName));
            SceneData.Billboards.Add(billboard);
            return billboard;
        }

        public Light CreateLight(LightType type)
        {
            Light light = new Light(type);
            SceneData.Lights.Add(light);
            return light;
        }


        public ParticleSystem CreateParticleSystem(GraphicsDevice device)
        {
            ParticleSystem ParticleSystem = new ParticleSystem(device, "smoke");
            SceneData.ParticleSystems.Add(ParticleSystem);
            return ParticleSystem;
        }

        public void Update(float deltaTime)
        {
            for (int i = 0; i < SceneData.ParticleSystems.Count; i++)
            {
                SceneData.ParticleSystems[i].Update(deltaTime);
            }
        }

        public void RenderScene()
        {
            _renderer.ViewMatrix = SceneData.ViewMatrix;
            _renderer.ProjectionMatrix = SceneData.ProjectionMatrix;
            _renderer.EyePosition = SceneData.EyePosition;

            _visibleAreaMgr.PrepareVisibleAreaData(_viewData, SceneData);

            _renderer.Render(_viewData);
        }

        private VisibleAreaData _viewData = new VisibleAreaData();
        private VisibleAreaManager _visibleAreaMgr = new VisibleAreaManager();

        private Renderer _renderer;
        public ContentManager Content { get; private set; }

        private Dictionary<string, ModelObject> _modelMap = new Dictionary<string, ModelObject>();
    }
}
